/*
 * 版权所有 (c) 2022-2099 TBIT。保留所有权利。
 */

package com.tbit.main.service.impl;

import cn.hutool.core.text.StrPool;
import cn.hutool.core.util.StrUtil;
import com.tbit.main.constant.BrandRemoteConfigConstant;
import com.tbit.main.pojo.BrandRemoteConfig;
import com.tbit.main.sms.enums.template.ApiStoreSmsTemplateEnum;
import com.tbit.main.sms.enums.template.UQSmsTemplateEnum;
import com.tbit.main.pojo.AccountSmsModel;
import com.tbit.main.pojo.BrandConfigBO;
import com.tbit.main.pojo.SmsSendMessage;
import com.tbit.main.service.BrandRemoteConfigService;
import com.tbit.main.service.DataService;
import com.tbit.main.service.SmsSendService;
import com.tbit.main.sms.client.SmsClient;
import com.tbit.main.sms.client.SmsClientFactory;
import com.tbit.main.sms.client.dto.SmsSendReqDTO;
import com.tbit.main.sms.enums.SmsChannelEnum;
import com.tbit.main.sms.properties.SmsChannelProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author <a href="mailto:li.yao@tbit.com.cn">yao</a>
 * @version 4.1.0
 * @since 2024/7/12 17:25
 */
@Slf4j
@Service
public class SmsSendServiceImpl implements SmsSendService {

    @Resource
    private SmsClientFactory smsClientFactory;
    @Resource
    private BrandRemoteConfigService brandRemoteConfigService;
    @Resource
    private DataService dataService;


    @Override
    public void sendSms(SmsSendMessage smsSendMessage) {
        SmsClient smsClient = createSmsClient(smsSendMessage.getBrandId(), smsSendMessage.getTemplateCode());

        if (smsClient == null) {
            log.warn("[sendSms][accountId({})未配置短信渠道]", smsSendMessage.getBrandId());
            return;
        }

        smsClient.send(new SmsSendReqDTO()
                .setMobile(smsSendMessage.getMobile())
                .setTemplateParams(smsSendMessage.getTemplateParams()));
    }

    public SmsClient createSmsClient(Integer brandId, String templateCode) {

        // uq
        BrandConfigBO brandConfig = brandRemoteConfigService.getConfigTbit(brandId);
        Integer uqTemplateId;
        UQSmsTemplateEnum uqSmsTemplate;

        // 创蓝
        BrandRemoteConfig chuangLanConfig;

        // 寻程
        AccountSmsModel accountSmsModel;
        ApiStoreSmsTemplateEnum apiStoreSmsTemplate;

        SmsChannelProperties properties = new SmsChannelProperties();
        properties.setApiTemplateCode(templateCode);
        if (brandConfig != null
                && ((uqSmsTemplate = UQSmsTemplateEnum.getTemplate(templateCode)) != null)
                && ((uqTemplateId = brandRemoteConfigService.getTemplateIdTbit(brandId, uqSmsTemplate.getTemplateId()))) != null) {
            properties.setCode(SmsChannelEnum.UQ.getCode());
            properties.setApiKey(brandConfig.getAppId());
            properties.setSignature(brandConfig.getNonce());
            properties.setApiSecret(brandConfig.getAppSecret());
            properties.setApiTemplateId(uqTemplateId);
        } else if ((chuangLanConfig = brandRemoteConfigService.getByAccountIdAndType(brandId, BrandRemoteConfigConstant.CL_SMS)) != null) {
            String[] keyValue = chuangLanConfig.getParamKV().split("&");
            String accountSms = keyValue[0].substring(keyValue[0].lastIndexOf("=") + 1);
            String passwordSms = keyValue[1].substring(keyValue[1].lastIndexOf("=") + 1);
            properties.setCode(SmsChannelEnum.CHUANG_LAN.getCode());
            properties.setApiKey(accountSms);
            properties.setApiSecret(passwordSms);
        } else if ((apiStoreSmsTemplate = ApiStoreSmsTemplateEnum.getTemplate(templateCode)) != null
                && (accountSmsModel = dataService.getAccountSmsModel(brandId + StrPool.COLON + apiStoreSmsTemplate.getTemplateId())) != null) {
            properties.setCode(SmsChannelEnum.API_STORE.getCode());
            properties.setApiKey(accountSmsModel.getSmsCodeKey());
            properties.setApiTemplateId(Integer.valueOf(accountSmsModel.getModelId()));
        }

        return StrUtil.isNotBlank(properties.getCode())
                ? smsClientFactory.getSmsClient(properties)
                : null;
    }
}
